use std::num::ParseIntError;

/// 将数字字符串解析为i64
pub fn parse_number_to_i64(s: &String) -> Result<i64, String> {
    let s = s.trim();

    if s.is_empty() {
        return Err("Empty string".to_string());
    }

    // 处理符号
    let (sign, rest) = if let Some(stripped) = s.strip_prefix('-') {
        (-1, stripped)
    } else if let Some(stripped) = s.strip_prefix('+') {
        (1, stripped)
    } else {
        (1, s)
    };

    // 根据前缀判断进制
    let result = if let Some(hex_rest) = rest.strip_prefix("0x").or_else(|| rest.strip_prefix("0X"))
    {
        // 16进制: 0x1A, 0XFF
        i64::from_str_radix(hex_rest, 16)
    } else if let Some(bin_rest) = rest.strip_prefix("0b").or_else(|| rest.strip_prefix("0B")) {
        // 2进制: 0b1101
        i64::from_str_radix(bin_rest, 2)
    } else if rest.starts_with('0') && rest != "0" {
        // 8进制: 077, 0123 (排除单独的0)
        i64::from_str_radix(&rest[1..], 8)
    } else {
        // 10进制
        rest.parse::<i64>()
    };

    result
        .map(|n| sign * n)
        .map_err(|e: ParseIntError| format!("Failed to parse '{}': {}", s, e))
}

// /// 批量转换函数
// pub fn parse_numbers(numbers: &[&str]) -> Vec<Result<i64, String>> {
//     numbers
//         .iter()
//         .map(|&s| parse_number_to_i64(s.to_string()))
//         .collect()
// }
//
// /// 带默认值的转换
// pub fn parse_number_or_default(s: &str, default: i64) -> i64 {
//     parse_number_to_i64(s.to_string()).unwrap_or(default)
// }

// 测试函数
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_decimal_numbers() {
        assert_eq!(parse_number_to_i64(&"123".to_string()), Ok(123));
        assert_eq!(parse_number_to_i64(&"-456".to_string()), Ok(-456));
        assert_eq!(parse_number_to_i64(&"+789".to_string()), Ok(789));
        assert_eq!(parse_number_to_i64(&"0".to_string()), Ok(0));
    }

    #[test]
    fn test_hexadecimal_numbers() {
        assert_eq!(parse_number_to_i64(&"0x1A".to_string()), Ok(26));
        assert_eq!(parse_number_to_i64(&"0XFF".to_string()), Ok(255));
        assert_eq!(parse_number_to_i64(&"-0x10".to_string()), Ok(-16));
        assert_eq!(parse_number_to_i64(&"0x0".to_string()), Ok(0));
    }

    #[test]
    fn test_octal_numbers() {
        assert_eq!(parse_number_to_i64(&"077".to_string()), Ok(63));
        assert_eq!(parse_number_to_i64(&"0123".to_string()), Ok(83));
        assert_eq!(parse_number_to_i64(&"-044".to_string()), Ok(-36));
        assert_eq!(parse_number_to_i64(&"00".to_string()), Ok(0));
    }

    #[test]
    fn test_binary_numbers() {
        assert_eq!(parse_number_to_i64(&"0b1101".to_string()), Ok(13));
        assert_eq!(parse_number_to_i64(&"0B1010".to_string()), Ok(10));
        assert_eq!(parse_number_to_i64(&"-0b1111".to_string()), Ok(-15));
    }

    #[test]
    fn test_invalid_numbers() {
        assert!(parse_number_to_i64(&"".to_string()).is_err());
        assert!(parse_number_to_i64(&"abc".to_string()).is_err());
        assert!(parse_number_to_i64(&"0xZZ".to_string()).is_err());
        assert!(parse_number_to_i64(&"08".to_string()).is_err()); // 8不是有效的八进制数字
    }
}
